package com.studymaster.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FileDownServet extends HttpServlet {
	
	@Override  
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
        doPost(req, resp);  
    }  
  
    @Override  
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)  
            throws ServletException, IOException {  
  
        File file = new File(getServletContext().getRealPath("/view")  
                + "/hello.jsp");  
  
        FileInputStream in = new FileInputStream(file);  
        OutputStream out = resp.getOutputStream();  
  
        resp.setContentType("Application/Octet-stream;charset=utf-8");  
        resp.addHeader("Content-Disposition",  
                "attachment; filename="+file.getName());  
        resp.addHeader("Content-Length::", file.length() + "");  
  
        byte[] b = new byte[1024];  
        int len = 0;  
        while ((len = in.read(b)) != -1) {  
            out.write(b, 0, len);  
        }  
  
        out.close();  
        in.close();  
  
    }  

}
